#include "stdafx.h"
#include "LayoutProperty.h"
#include "LayoutEditor.h"
#include "AllLookFeel.h"

LayoutProperty::LayoutProperty()
{
	mLayout = new MGUI_Layout(AllLookFeel::Instance()->GetPanel());

	int top = 0;
	const int K_Space = 8;

	// Name
	//
	MGUI_Label * lb_Name = new MGUI_Label(NULL, mLayout);
	lb_Name->SetCaption(L"Name:");
	lb_Name->SetRect(MGUI_Rect(true, 0, top , 64, 24));

	mEditBox_Name = new MGUI_EditBox(AllLookFeel::Instance()->GetEditBox(), mLayout);
	mEditBox_Name->SetCaption(L"");
	mEditBox_Name->SetRect(MGUI_Rect(true, 80, top, 165, 24));
	top += 24 + K_Space;

	// Rect
	//
	MGUI_Label * lb_Rect = new MGUI_Label(NULL, mLayout);
	lb_Rect->SetCaption(L"Rect:");
	lb_Rect->SetRect(MGUI_Rect(true, 0, top, 64, 24));

	mEditBox_Rect = new MGUI_EditBox(AllLookFeel::Instance()->GetEditBox(), mLayout);
	mEditBox_Rect->SetCaption(L"0 0 1 1");
	mEditBox_Rect->SetRect(MGUI_Rect(true, 80, top, 165, 24));

	top += 24 + K_Space;

	// Order
	//
	MGUI_Label * lb_Order = new MGUI_Label(NULL, mLayout);
	lb_Order->SetCaption(L"Order:");
	lb_Order->SetRect(MGUI_Rect(true, 0, top, 64, 24));

	mEditBox_Order = new MGUI_EditBox(AllLookFeel::Instance()->GetEditBox(), mLayout);
	mEditBox_Order->SetCaption(L"0 0 1 1");
	mEditBox_Order->SetRect(MGUI_Rect(true, 80, top, 165, 24));

	top += 24 + K_Space;

	// Color
	//
	MGUI_Label * lb_TextColor = new MGUI_Label(NULL, mLayout);
	lb_TextColor->SetCaption(L"Color:");
	lb_TextColor->SetRect(MGUI_Rect(true, 0, top, 64, 24));

	mWidget_Color = new MGUI_Widget(AllLookFeel::Instance()->GetWhite(), mLayout);
	mWidget_Color->SetRect(MGUI_Rect(true, 80, top, 165, 24));

	top += 24 + K_Space;

	// H Align
	//
	MGUI_Label * lb_HAlign = new MGUI_Label(NULL, mLayout);
	lb_HAlign->SetCaption(L"HAlign:");
	lb_HAlign->SetRect(MGUI_Rect(true, 0, top, 64, 24));

	mComboBox_HAlign = new MGUI_ComboBox(AllLookFeel::Instance()->GetComboBox(), mLayout);
	mComboBox_HAlign->SetRect(MGUI_Rect(true, 80, top, 165, 24));

	top += 24 + K_Space;

	// V Align
	//
	MGUI_Label * lb_VAlign = new MGUI_Label(NULL, mLayout);
	lb_VAlign->SetCaption(L"VAlign:");
	lb_VAlign->SetRect(MGUI_Rect(true, 0, top, 64, 24));

	mComboBox_VAlign = new MGUI_ComboBox(AllLookFeel::Instance()->GetComboBox(), mLayout);
	mComboBox_VAlign->SetRect(MGUI_Rect(true, 80, top, 165, 24));

	top += 24 + K_Space;

	// Enable
	//
	MGUI_Label * lb_Enable = new MGUI_Label(NULL, mLayout);
	lb_Enable->SetCaption(L"Enable:");
	lb_Enable->SetRect(MGUI_Rect(true, 0, top, 64, 24));

	mComboBox_Enable = new MGUI_ComboBox(AllLookFeel::Instance()->GetComboBox(), mLayout);
	mComboBox_Enable->SetRect(MGUI_Rect(true, 80, top, 165, 24));

	top += 24 + K_Space;

	// Pick Enable
	//
	MGUI_Label * lb_PickEnable = new MGUI_Label(NULL, mLayout);
	lb_PickEnable->SetCaption(L"Pick:");
	lb_PickEnable->SetRect(MGUI_Rect(true, 0, top, 64, 24));

	mComboBox_PickFlag = new MGUI_ComboBox(AllLookFeel::Instance()->GetComboBox(), mLayout);
	mComboBox_PickFlag->SetRect(MGUI_Rect(true, 80, top, 165, 24));

	top += 24 + K_Space;


	// Init
	//
	mComboBox_HAlign->Append(L"None");
	mComboBox_HAlign->Append(L"Left");
	mComboBox_HAlign->Append(L"Center");
	mComboBox_HAlign->Append(L"Right");
	mComboBox_HAlign->Append(L"Stretch");

	mComboBox_VAlign->Append(L"None");
	mComboBox_VAlign->Append(L"Top");
	mComboBox_VAlign->Append(L"Center");
	mComboBox_VAlign->Append(L"Bottom");
	mComboBox_VAlign->Append(L"Stretch");

	mComboBox_Enable->Append(L"True");
	mComboBox_Enable->Append(L"False");

	mComboBox_PickFlag->Append(L"All");
	mComboBox_PickFlag->Append(L"Self");
	mComboBox_PickFlag->Append(L"Child");
	mComboBox_PickFlag->Append(L"None");

	mComboBox_HAlign->SetSelectIndex(0);
	mComboBox_VAlign->SetSelectIndex(0);
	mComboBox_Enable->SetSelectIndex(0);
	mComboBox_PickFlag->SetSelectIndex(0);

	
	// register event
	mEditBox_Name->eventKeyLostFocus +=  OnNameChanged(this, &LayoutProperty::OnNameChanged_);
	mEditBox_Rect->eventKeyLostFocus += OnRectChanged(this, &LayoutProperty::OnRectChanged_);
	mEditBox_Order->eventKeyLostFocus += OnOrderChanged(this, &LayoutProperty::OnOrderChanged_);
	mComboBox_HAlign->eventSelectChanged += OnHAlignChanged(this, &LayoutProperty::OnAlignChanged_);
	mComboBox_VAlign->eventSelectChanged += OnVAlignChanged(this, &LayoutProperty::OnAlignChanged_);
	mComboBox_Enable->eventSelectChanged += OnEnableChanged(this, &LayoutProperty::OnEnableChanged_);
	mComboBox_PickFlag->eventSelectChanged += OnPickFlagChanged(this, &LayoutProperty::OnPickFlagChanged_);

	LayoutEditor::Instance()->eventEditWidgetChanged += OnEditWidgetChanged(this, &LayoutProperty::OnEditWidgetChanged_);
	LayoutEditor::Instance()->eventRectChanged += OnEditRectChanged(this, &LayoutProperty::OnEditRectChanged_);
}

LayoutProperty::~LayoutProperty()
{
	delete mLayout;
}

void LayoutProperty::Layout()
{
	MGUI_Rect rect;

	rect.x0 = MGUI_Engine::Instance()->GetRect().x1 - 256;
	rect.x1 = MGUI_Engine::Instance()->GetRect().x1;

	rect.y0 = 32;
	rect.y1 = MGUI_Engine::Instance()->GetRect().y1 - 32;

	mLayout->SetRect(rect);
}

void LayoutProperty::OnNameChanged_(MGUI_Widget *)
{
	TString128 name = TString128::FromUnicode(mEditBox_Name->GetCaption().c_str());

	LayoutEditor::Instance()->notifyNameChanged(name);
}

void LayoutProperty::OnRectChanged_(MGUI_Widget *)
{
	const MGUI_String & text = mEditBox_Rect->GetCaption();
	TString128 strRect = TString128::FromUnicode(text.c_str());

	MGUI_Rect rect;

	rect.FromString(strRect);

	LayoutEditor::Instance()->notifyRectChanged(rect);
}

void LayoutProperty::OnOrderChanged_(MGUI_Widget *)
{
	MGUI_Widget * widget = LayoutEditor::Instance()->GetEditWidget();

	if (widget == NULL)
		return ;

	const MGUI_String & text = mEditBox_Rect->GetCaption();
	TString128 str = TString128::FromUnicode(text.c_str());

	int order = str.ToInt();

	widget->SetOrder(order);
}

void LayoutProperty::OnColorChanged_()
{
}

void LayoutProperty::OnAlignChanged_(int _index)
{
	MGUI_Widget * widget = LayoutEditor::Instance()->GetEditWidget();

	if (widget == NULL)
		return ;

	int hIndex = mComboBox_HAlign->GetSelectIndex();
	int vIndex = mComboBox_VAlign->GetSelectIndex();

	if (hIndex == -1 || vIndex == -1)
		return ;

	const int hAlign[] = {
		MGUI_Align::None,
		MGUI_Align::Left,
		MGUI_Align::HCenter,
		MGUI_Align::Right,
		MGUI_Align::HStretch
	};

	const int vAlign[] = {
		MGUI_Align::None,
		MGUI_Align::Top,
		MGUI_Align::VCenter,
		MGUI_Align::Bottom,
		MGUI_Align::VStretch
	};

	int flag = hAlign[hIndex] | vAlign[vIndex];

	widget->SetAlign(flag);
}

void LayoutProperty::OnEnableChanged_(int _index)
{
	MGUI_Widget * widget = LayoutEditor::Instance()->GetEditWidget();

	if (widget == NULL)
		return ;

	bool enable = (_index == 0);
	widget->SetEnable(enable);
}

void LayoutProperty::OnPickFlagChanged_(int _index)
{
	MGUI_Widget * widget = LayoutEditor::Instance()->GetEditWidget();

	if (widget == NULL)
		return ;

	int flag[] = {
		MGUI_PickFlag::All,
		MGUI_PickFlag::Self,
		MGUI_PickFlag::Child,
		MGUI_PickFlag::None
	};

	widget->SetPickFlag(flag[_index]);
}

void LayoutProperty::OnEditWidgetChanged_(MGUI_Widget * _widget)
{
	if (_widget != NULL)
	{
		mEditBox_Name->SetCaption(_widget->GetName().c_wstr());
		mEditBox_Rect->SetCaption(_widget->GetRect().ToString().c_wstr());
		mEditBox_Order->SetCaption(TString128(_widget->GetOrder()).c_wstr());

		mWidget_Color->SetColor(_widget->GetColor());

		int align = _widget->GetAlign()._value;

		if (align & MGUI_Align::Left)
			mComboBox_HAlign->SetSelectIndex(1);
		else if (align & MGUI_Align::HCenter)
			mComboBox_HAlign->SetSelectIndex(2);
		else if (align & MGUI_Align::Right)
			mComboBox_HAlign->SetSelectIndex(3);
		else if (align & MGUI_Align::HStretch)
			mComboBox_HAlign->SetSelectIndex(4);
		else
			mComboBox_HAlign->SetSelectIndex(0);

		if (align & MGUI_Align::Top)
			mComboBox_VAlign->SetSelectIndex(1);
		else if (align & MGUI_Align::VCenter)
			mComboBox_VAlign->SetSelectIndex(2);
		else if (align & MGUI_Align::Bottom)
			mComboBox_VAlign->SetSelectIndex(3);
		else if (align & MGUI_Align::VStretch)
			mComboBox_VAlign->SetSelectIndex(4);
		else
			mComboBox_VAlign->SetSelectIndex(0);

		if (mComboBox_Enable->GetEnable())
			mComboBox_Enable->SetSelectIndex(0);
		else
			mComboBox_Enable->SetSelectIndex(1);

		int pk = _widget->GetPickFlag();

		if (pk == MGUI_PickFlag::All)
			mComboBox_PickFlag->SetSelectIndex(0);
		else if (pk == MGUI_PickFlag::Self)
			mComboBox_PickFlag->SetSelectIndex(1);
		else if (pk == MGUI_PickFlag::Child)
			mComboBox_PickFlag->SetSelectIndex(2);
		else
			mComboBox_PickFlag->SetSelectIndex(3);
	}
	else
	{
	}
}

void LayoutProperty::OnEditRectChanged_(MGUI_Rect rect)
{
	mEditBox_Rect->SetCaption(rect.ToString().c_wstr());
}
